//
//  IntroduceViewController.m
//  DLTravels
//
//  Created by mac1012 on 2016/12/13.
//  Copyright © 2016年 DianlE. All rights reserved.
//

#import "IntroduceViewController.h"
#import <NYXImagesKit/NYXImagesKit.h>
#import "LoginViewController.h"
#import "BaseNavigationController.h"
#import "SMPageControl.h"

@interface IntroduceViewController (){
  
    NSMutableArray    *_imageViews;
    
}
@property (nonatomic,strong) SMPageControl *pageControl;

@end

@implementation IntroduceViewController

- (NSUInteger)numberOfPages{
    
    return 3;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    

    _imageViews = [[NSMutableArray alloc] initWithCapacity:3];
    [self configureAnimation];
    [self configureAnimations];
}

- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];
    
}

- (UIStatusBarStyle )preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
}

- (void)configureAnimation{

    [self configurePageControl];
    
    CGFloat scaleFactor = 1.0;
    CGFloat desginHeight = 568.0;//iPhone5s 的设计尺寸
    if (!kDevice_Is_iPhone5 && !kDevice_Is_iPhone5) {
        scaleFactor = kScreen_Height/desginHeight;
    }
    
   NSArray *introduceImages = @[@"Introduce_1",@"Introduce_2",@"Introduce_3"];

    for (NSString *imageName in introduceImages) {
        
        UIImage *img = [UIImage imageNamed:imageName];
        img = scaleFactor != 1.0? [img scaleByFactor:scaleFactor] : img;
        UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
       
        [self.contentView addSubview:imgView];
        [_imageViews addObject:imgView];
        
    }

}

- (void)pushLoginController{
    
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:loginVC];
    
    [self presentViewController:nav animated:YES completion:nil];
    
}

#pragma mark Super
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self animateCurrentFrame];
    NSLog(@"%lf",scrollView.contentOffset.x);
    NSInteger maxOffsetX = self.pageWidth*(self.numberOfPages-1);
    CGFloat   offsetX = scrollView.contentOffset.x - maxOffsetX;
    if(offsetX > 45){
        [self pushLoginController];
        return;
    }
    
    NSInteger nearestPage = floorf(scrollView.contentOffset.x/self.pageWidth);
    self.pageControl.currentPage = nearestPage;
    
}





- (void)configureAnimations
{
    [self configureScrollViewAnimations];
    [self configureImageView];
    
}

- (void)configureImageView{
    
  
    for (NSInteger index = 0; index<[self numberOfPages]; index++) {
        
//        if (index !=0) {
        
        
        UIImageView *imgView = [_imageViews objectAtIndex:index];
        
        [self keepView:[_imageViews objectAtIndex:index] onPage:index];
        
        IFTTTAlphaAnimation *iconAlphaAnimation = [IFTTTAlphaAnimation animationWithView:imgView];
        [iconAlphaAnimation addKeyframeForTime:index -kScreen_Width alpha:0.f];
        [iconAlphaAnimation addKeyframeForTime:index alpha:1.f];
        [iconAlphaAnimation addKeyframeForTime:index +kScreen_Width alpha:0.f];
        [self.animator addAnimation:iconAlphaAnimation];
        
//        }
    }
}


- (void)configureScrollViewAnimations
{
    // change the scrollView's background color from dark gray to blue just after page 1
    IFTTTBackgroundColorAnimation *backgroundColorAnimation = [IFTTTBackgroundColorAnimation animationWithView:self.scrollView];
    [backgroundColorAnimation addKeyframeForTime:1 color:[UIColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.f]];
    [backgroundColorAnimation addKeyframeForTime:1.1 color:[UIColor colorWithRed:0.14f green:0.8f blue:1.f alpha:1.f]];
    [self.animator addAnimation:backgroundColorAnimation];
}

- (void)configurePageControl{
    
    self.pageControl = [[SMPageControl alloc] init];
    self.pageControl.userInteractionEnabled = NO;
    self.pageControl.numberOfPages = [self numberOfPages];
    self.pageControl.pageIndicatorImage = [UIImage imageNamed:@"Introduce_dot_unselected"];
    self.pageControl.currentPageIndicatorImage = [UIImage imageNamed:@"Introduce_dot_selected"];
    self.pageControl.currentPage = 0;
    
    [self.view addSubview:self.pageControl];
    
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, kScaleFrom_iPhone5_Desgin(20)));
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(-kScaleFrom_iPhone5_Desgin(20));
    }];
    
}

@end
